package cn.hiapi.core.basic;

import cn.hiapi.core.basic.abs.AbsResponse;
import cn.hiapi.core.basic.response.DefaultResponseCode;
import cn.hiapi.core.basic.response.ResponseEntity;
import cn.hiapi.core.basic.response.ResponseUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

/**
 * @author AdinZ
 * @date 2024/1/12 10:39
 */
public class ResUtils extends Nuwa {

    public static BigDecimal getDefault(BigDecimal decimal) {
        return decimal == null ? BigDecimal.ZERO : decimal;
    }

    public static BigInteger getDefault(BigInteger decimal) {
        return decimal == null ? BigInteger.ZERO : decimal;
    }

    public static long getDefault(Long l) {
        return l == null ? 0L : l;
    }


    public static int getDefault(Integer i) {
        return i == null ? 0 : i;
    }

    public static float getDefault(Float i) {
        return i == null ? 0 : i;
    }

    public static double getDefault(Double i) {
        return i == null ? 0 : i;
    }

    public static boolean getDefault(Boolean i) {
        return i != null && i;
    }

    public static BigDecimal decimal(BigDecimal amount, int decimal) {
        if (amount == null) amount = BigDecimal.ZERO;
        return amount.setScale(decimal, RoundingMode.HALF_EVEN);
    }

    public static <T> T getDefault(T value, T def) {
        if (value == null) return def;
        return value;
    }



    public static <T> ResponseEntity<T> toResult(int code, String message, T data) {
        ResponseEntity<T> response = new ResponseEntity<>();
        response.setCode(code);
        response.setMessage(message);
        response.setData(data);
        return response;
    }

    public static <T> ResponseEntity<T> toError(int code, String message) {
        return toResult(code, message, null);
    }

    public static <T> ResponseEntity<T> toError(String message) {
        return toError(300, message);
    }

    public static <T> ResponseEntity<T> toError() {
        return toError("参数错误");
    }

    public static <T> ResponseEntity<T> toSuccess(String message, T data) {
        return toResult(DefaultResponseCode.SUCCESS.getCode(),message,data);
    }

    public static <T> ResponseEntity<T> toSuccess(T data) {
        return toSuccess(DefaultResponseCode.SUCCESS.getRemark(), data);
    }

    public static <T> ResponseEntity<T> toSuccess() {
        return toSuccess(null);
    }


}
